< previous page page_337 next page >

Page 337
Error-handling Techniques
At runtime, different things can happen that cause an error condition in your program. For example, a file could get deleted or a database file is unable to be opened for some reason or is not in the location it should be. If this situation isnt handled in your code, your program will bomb. Visual Basic supplies an error-handling method that allows you to trap and deal with these unwanted errors.
To perform error handling in Visual Basic, you must complete three steps:
Tell Visual Basic what to do if an error occurs
Write some code that will handle, or fix, the error
Return back to what you were doing when the error occurred or continue on with the program
On Error GoTo <LabelName>
The statement On Error Go To <LabelName> tells Visual Basic that from this point forward in the program, if an error occurs, the program should jump to the label identified. Youll see this form of error handling most often. The label name you use must be within the same procedure.
Examples Filename: Divide.frm
In the form shown in Figure 17.2, three different forms of error handling are demonstrated.
8160-0337a.gif
Figure 17.2
Error handling techniques.
The first method is to just let Visual Basic handle the error. The second method shows you how to use the On Error GoTo <LabelName> option. Lets look at the code under the GoTo Label command button:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdGotoLabel_Click()
   On Error GoTo Error_EH

   Stop

 
< previous page page_337 next page >

If you like this book, buy it!